package com.example.admin.brainwave.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.admin.brainwave.R;
import com.example.admin.brainwave.base.BaseActivity;
import com.example.admin.brainwave.bean.CurrencyBean;
import com.example.admin.brainwave.net.LoginByPassTask;
import com.example.admin.brainwave.utils.Const;
import com.example.admin.brainwave.utils.SPHelper;
import com.example.admin.brainwave.utils.ToastUtil;

import butterknife.BindView;

public class LoginForPassActivity extends BaseActivity {


    @BindView(R.id.iv_login_language)
    ImageView ivLoginLanguage;
    @BindView(R.id.tv_login_language)
    TextView tvLoginLanguage;
    @BindView(R.id.rl_login_language)
    RelativeLayout rlLoginLanguage;
    @BindView(R.id.iv_login_phone)
    ImageView ivLoginPhone;
    @BindView(R.id.et_login_phone)
    EditText etLoginPhone;
    @BindView(R.id.ll_login_phone)
    LinearLayout llLoginPhone;
    @BindView(R.id.iv_login_pass)
    ImageView ivLoginPass;
    @BindView(R.id.et_login_pass)
    EditText etLoginPass;
    @BindView(R.id.ll_login_pass)
    LinearLayout llLoginPass;
    @BindView(R.id.btn_login_login)
    Button btnLoginLogin;
    @BindView(R.id.btn_login_reg)
    Button btnLoginReg;
    @BindView(R.id.tv_login_msg)
    TextView tvLoginMsg;
    @BindView(R.id.tv_login_forget)
    TextView tvLoginForget;
    private String phone;
    private String pass;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_login_pass;
    }


    @Override
    protected void initData() {
        String token = SPHelper.getInstance(this).getString("token", "");
        String userId = SPHelper.getInstance(this).getString("userId", "");
        if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(userId)) {
            startActivity(MainActivity.class);
            finish();
        }
    }

    @Override
    protected void addListener() {

        rlLoginLanguage.setOnClickListener(view -> startActivity(new Intent(LoginForPassActivity.this, SetLanguageActivity.class)));
        tvLoginMsg.setOnClickListener(view -> startActivity(new Intent(LoginForPassActivity.this, LoginForSmsActivity.class)));

        tvLoginForget.setOnClickListener(view -> startActivity(new Intent(LoginForPassActivity.this, ResetPassActivity.class)));

        btnLoginLogin.setOnClickListener(v -> {
            if (isEmpty()) {
                login();
            }
        });

        btnLoginReg.setOnClickListener(view -> startActivity(new Intent(LoginForPassActivity.this, RegisterActivity.class)));
    }


    private void login() {
        new LoginByPassTask(this, o -> {
            if (o != null) {
                CurrencyBean c = (CurrencyBean) o;
                if (Const.OKCODE.equals(c.getCode())) {
                    SPHelper.getInstance(LoginForPassActivity.this).putString("token", c.getResult());
                    SPHelper.getInstance(LoginForPassActivity.this).putString("userId", etLoginPhone.getText().toString());
                    startActivity(new Intent(LoginForPassActivity.this, MainActivity.class));
                    finish();
                } else {
                    ToastUtil.showShort(LoginForPassActivity.this, c.getMessage());
                }
            }
        }).execute(phone, pass);
    }


    private boolean isEmpty() {
        phone = etLoginPhone.getText().toString();
        pass = etLoginPass.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showShort(this, getResources().getString(R.string.phone) + " " + getResources().getString(R.string.not_null));
            return false;
        }
        if (TextUtils.isEmpty(pass)) {
            ToastUtil.showShort(this, getResources().getString(R.string.password) + " " + getResources().getString(R.string.not_null));
            return false;
        }
        return true;
    }

}
